/**   
 * @Title: ConnectForHealthNews.java 
 * @Package cn.edu.cdtu.healthclient.connect 
 * @Description: TODO 
 * @author 
 * @date 
 * @version V0.0   
 */
package cn.edu.cdtu.healthclient.connect;

import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import cn.edu.cdtu.healthclient.domain.HealthNewsJson;
import cn.edu.cdtu.healthclient.utils.ConnectUtil;

import com.google.gson.Gson;

/** 
 * @ClassName: ConnectForHealthNews 
 * @Description: TODO
 * @author 
 * @date 
 *  
 */
public class ConnectForHealthNews 
{
	/**
	 * 
	 * @Title: connect 
	 * @Description: 通过指定的URL来访问服务器 获得健康新闻学习。
	 * @return HealthNewsJson
	 * @throws 
	 *
	 * @param id
	 * @param operation
	 * @param httpClient
	 * @return
	 * @throws IOException
	 */
	public static HealthNewsJson connect(String id,String operation,
			HttpClient httpClient) throws IOException
	{
		//健康新闻信息的JSON对象引用。
		HealthNewsJson healthNewsJson = null;
		//根据参数进行Http访问
		HttpGet httpGet = new HttpGet(
				ConnectUtil.HealthServerContect.SERVER_URL
				+ ConnectUtil.HealthServerContect.HEALTH_NEWS_SERVLET
				+ "?" + ConnectUtil.HealthNewsPargrams.Pargram.HEALTH_NEWS_ID
				+ "=" + id + "&"
				+ ConnectUtil.HealthNewsPargrams.Option.HEALTH_NEWS_OPERATION
				+ "="
				+ ConnectUtil.HealthNewsPargrams.Option.OptionValue.HEALTH_NEWS_OPERATION_BODY);
		//执行网络连接
		HttpResponse httpResponse = httpClient.execute(httpGet);
		//连接成功后从服务器获得健康新闻数据（数据以JSON进行封装）。
		if(httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
			String responseMessage = EntityUtils.toString(httpResponse.getEntity(),
					HTTP.UTF_8);
			Gson gson=new Gson();
			healthNewsJson = gson.fromJson(responseMessage,HealthNewsJson.class);
		}
		//返回数据对象。
		return healthNewsJson;
	}
}

